﻿using CarAppDto.Rule.RuleConfigDtos;
using CarAppDto.Rule.RuleMappingDtos;
using CarAppService.Rule.IServices;
using D.UtilCore;
using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using D.UtilSqlsugarOrm;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CarAppAPI.Rule
{
    /// <summary>
    /// 规则组成
    /// </summary>
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class RuleMappingController : BaseApiController
    {
        private readonly IRuleMappingEntityService _RuleMappingService;
        public RuleMappingController(IRuleMappingEntityService RuleMappingService)
        {
            _RuleMappingService = RuleMappingService;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqAddRuleMapping"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddRuleMapping reqAddRuleMapping)
        {
            // 验证和清理输入数据，防止注入攻击
            var sanitizedComment = System.Web.HttpUtility.HtmlEncode(reqAddRuleMapping.Descption);

            reqAddRuleMapping.CreateUser = AdminId;
            return await _RuleMappingService.AddAsync(reqAddRuleMapping);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _RuleMappingService.DeleteAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqUpdateRuleMapping"></param>
        /// <returns></returns>
        [HttpPut, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update( [FromBody] ReqUpdateRuleMapping reqUpdateRuleMapping)
        {
            reqUpdateRuleMapping.UpdateUser = AdminId;
            return await _RuleMappingService.UpdateAsync(reqUpdateRuleMapping);
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepRuleMapping>> Get(string? id)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = AdminId;
            }
            return await _RuleMappingService.GetAsync(id);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqGueryRuleMappingList"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepRuleMapping>>> List([FromBody] ReqGueryRuleMappingList reqGueryRuleMappingList)
        {
            return await _RuleMappingService.QueryListAsync(reqGueryRuleMappingList);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepRuleMapping>>> Pages([FromBody] ReqQueryRuleMappingPage reqQueryRuleMappingPage)
        {
            return await _RuleMappingService.QueryPagesAsync(reqQueryRuleMappingPage);
        }

        /// <summary>
        /// 根据VIN码获取规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepRuleMapping>>> GetRuleMappingByVIN(RepRuleConfigAndVIN rep)
        {
            return await _RuleMappingService.GetRuleMappingByVIN(rep);
        }
    }
}
